#!/usr/bin/env python
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Matt Layman"
__author__ = "Matt Layman <laymansterms.dev@gmail.com>"

import unittest
import sys
import os

sys.path += [os.path.abspath('..')]

from tests.Music_test import TestMusic

from frontend.backend_connection import BackendConnection
from frontend.medialibrary.music import MusicLibrary
from frontend.medialibrary.music import Album, Track

class MockBackendConnection(BackendConnection):
    def __init__(self): pass
    def close_connection(self): pass

class TestMusicLibrary(TestMusic):

    def setUp(self):
        TestMusic.setUp(self)
        try:
            self.backend_connection = MockBackendConnection()
        except:
            print "\nbackend_connection failed"
        self.musiclibrary = MusicLibrary(self.backend_connection, self.TEST_DB)

    def tearDown(self):
        self.backend_connection.close_connection()
        TestMusic.tearDown(self)

    def testMusicLibraryConstructor(self):
        """testMusicLibraryContructor - Ensures instantiation of MusicLibrary 
        class"""
        self.assertTrue(isinstance(self.musiclibrary, MusicLibrary))
        
    def testGetCompactDiscInformation(self):
        """testGetCompactDiscInformation - Ensures compact disc information
        is returned"""
        #TODO: write this test
        pass

    def testGetPlaylist(self):
        #TODO: write this test, set up the playlist test table
        pass

    def testGetNumberOfTracksOnPlaylist(self):
        #TODO: write this test, set up the playlist test table
        pass

    def testGetTracksOnPlaylist(self):
        #TODO: write this test, set up the playlist test table
        pass

    def testGetAllArtists(self):
        """testGetAllArtists - Ensures that all artists are returned from
        the music library"""
        result = self.musiclibrary.get_all_artists()
        self.assertEqual(result, ['artist0', 'artist1'])

    def testGetAllTracks(self):
        """testGetAllTracks - Ensures that all tracks are returned"""
        result = self.musiclibrary.get_all_tracks()
        for i in result:
            self.assertTrue(isinstance(i, Track))
        self.assertEqual(len(result), 8)

    def testGetTracksByGenre(self):
        """testGetTracksByGenre - Ensures tracks of a certain genre are 
        returned"""
        result = self.musiclibrary.get_tracks_by_genre('genre0')
        self.assertEqual(len(result), 4)
        for i in result:
            self.assertEqual(i.get_genre(), 'genre0')

    def testGetTracksByMissingGenre(self):
        """testGetTracksByMissingGenre - Ensures proper handling of a missing
        genre"""
        self.assertEquals(len(self.musiclibrary.get_tracks_by_genre('foo-bar-baz**')), 0)

    def testGetTracksByArtist(self):
        """testGetTracksByArtist - Ensure tracks by a certain artist are
        returned"""
        result = self.musiclibrary.get_tracks_by_artist('artist1')
        self.assertEqual(len(result), 4)
        for i in result:
            self.assertEqual(i.get_artist(), 'artist1')

    def testGetTracksByUnknownArtist(self):
        """testGetTracksByUnknownArtist - Ensure proper handling of an missing
        artist in the cache"""
        self.assertEquals(len(self.musiclibrary.get_tracks_by_artist('foo')), 0)

    def testGetAllAlbums(self):
        """testGetAllAlbums - Ensures all albums are returned"""
        result = self.musiclibrary.get_all_albums()
        for i in result:
            self.assertTrue(isinstance(i, Album))
        self.assertEqual(len(result), 2)
    
    def testGetAlbumsByArtist(self):
        """testGetAlbumsByArtist - Ensures correct albums by an artist is
        returned"""
        result = self.musiclibrary.get_albums_by_artist('artist0')
        self.assertEqual(len(result), 2)
        for i in result:
            self.assertEqual(i.get_artist(), 'artist0')

    def testGetAlbumsByUnknownArtist(self):
        """testGetAlbumsByUnknownArtist - Ensures proper handling of an
        artist not in the cache"""
        self.assertEquals(len(self.musiclibrary.get_albums_by_artist('foo')), 0)

    def testNumberOfTracks(self):
        """testNumberOfTracks - Ensures number of all tracks is returned"""
        result = self.musiclibrary.number_of_tracks()
        self.assertEqual(result, 8)

    def testNumberOfTracksByArtist(self):
        """testNumberOfTracksByArtist - Ensures number of all tracks by one
        artist is returned"""
        result = self.musiclibrary.number_of_tracks_by_artist('artist0')
        self.assertEqual(result, 4)

    def testNumberOfTracksByUnknownArtist(self):
        """testNumberOfTracksByUnknownArtist - Ensures proper handling when
        artist called is not in the cache"""
        self.assertEqual(self.musiclibrary.number_of_albums_by_artist('foo'), 0)

    def testNumberOfAlbumsByArtist(self):
        """testNumberOfAlbumsByArtist - Ensures number of all albums by one
        artist is returned"""
        result = self.musiclibrary.number_of_albums_by_artist('artist1')
        self.assertEqual(result, 2)

    def testNumberOfAlbumsByUnknownArtist(self):
        """testNumberOfAlbumsByUnknownArtist - Ensures proper handling when
        artist called is not in the cache"""
        self.assertEqual(self.musiclibrary.number_of_albums_by_artist('foo'), 0)
        
    def testSaveLyrics(self):
        """testSaveLyrics - Ensures lyrics for a track are saved in the
        database"""
        # Only need a filename that matches something in the database, the rest
        # of the track is for construction purposes only
        self.track = Track('/filename/000', '', 0, '', '', '', '', 0, 1, 0, '',
                           '')
        self.musiclibrary.save_lyrics(self.track, 'some lyrics here')
        self.assertEqual(self.track.get_lyrics(), 'some lyrics here')
        #TODO: create second half of this test to check that lyrics were saved
        # to storage by creating a new track from the database and checking its
        # lyrics value
        
